import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime
import random

class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def tset_shop_list(self):
      
        # 点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        self.browser.find_element(By.XPATH, '//*[@id="keyword"]').send_keys('文具')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()
        
        # 接收列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        # 接收条数
        tr_count = len(tr_list)
        if tr_count == 0:
            print('没有数据')
        is_not_bug = True
        for i in tr_list:
            shop_name = i.find_element(By.XPATH, './td[2]/div').text
            if '文具' not in shop_name:
                is_not_bug = False
        if is_not_bug:
            print('商品列表查询没有问题')
        else:
            print('商品列表有问题')
        sleep(2)
        
    def test_login(self):
      
        self.browser.get('http://www.likeshops.localhost/admin/index/index.html')
        sleep(2)
        
        # 输入用户名
        self.browser.find_element(By.NAME, 'account').send_keys('admin')
        
        # 输入密码
        self.browser.find_element(By.NAME, 'password').send_keys('admin')
        
        # 输入验证码
        self.browser.find_element(By.NAME, 'code').send_keys('111111')
        
        # 点击登录
        self.browser.find_element(By.ID, 'login').click()
        sleep(3)
        
    def test_shop_add(self):
   
        # 点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位商品子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        # 点击发布商品按钮
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        
        # 定位商品发布子框架
        shop_add_iframe = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 获取当前时间
        now = datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
        # 定位商品名称并填写信息
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品' + now)
        
        sleep(3)
        # 点击第一个大类的下拉框
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(2)
        # 点击第一个大类的下拉数据的第一个数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(2)
        # 点击第二个类的下拉框
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(2)
        # 点击第二个类的下拉数据的第一个数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(2)
        # 点击第三个类的下拉框
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(2)
        # 点击第三个类的下拉数据的第一个数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        
        # 商品主图
        # 定位商品主图按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(2)
        
        # 进入到上传图片子框架
        # 定位上传图片子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_1)
        
        self.test_upload_img('C:\\Users\\韦帮愫\\Desktop\\software-testing\\验证码.png')
        
        # 商品轮播图
        # 回到商品发布子框架 先回到主框架，再进入商品子框架、最后进入商品发布子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 定位商品轮播图 图片上传
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(2)
        # 进入图片上传子框架
        upload_img_iframe_2 = self.browser.find_element(By.XPATH, '/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('C:\\Users\\韦帮愫\\Desktop\\software-testing\\验证码.png')
        
        # 回到商品发布子框架, 先回到主框架，再进入商品子框架、最后进入商品发布子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 点击价格库存
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(2)
        
        # 定位价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        sleep(3)
        
        # 点击保存按钮，因为保存是在商品子框架，所以需要回到商品子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
        
    def test_shop_sj(self):
       
        self.browser.switch_to.default_content()
        # 点击商品
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位子框架
        iframe1 = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        # 点击仓库中商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        
        #获取列表
        shop_list =self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr[1]')
        if len(shop_list)!=0:
            # 列表长度不为零则上架
            # 不进行循环，因为上架操作后列表会刷新，固定第一条数据操作
            
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(2)
            self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
            sleep(2)
            
    def test_upload_img(self, file_path):
        '''
        图片上传
        '''
        # 通过文件输入框完成图片上传操作
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div[1]/input').send_keys(file_path)
        sleep(5)
        # 选中第一张图片
        self.browser.find_element(By.XPATH, '//*[@id="lists"]/li[1]/div/img').click()
        sleep(1)
        # 点击选中使用图片按钮
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        sleep(2)
    
    def test_shop_edit(self):
        # 点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位商品子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        #点击第一个商品的编辑按钮
        self.browser.find_element(By.XPATH, ' /html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
        
        #定位商品修改子框架
        sleep(2)
        shop_edit_iframe = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe1"]')
        #/html/body/div[3]/div[2]/iframe
        self.browser.switch_to.frame(shop_edit_iframe)
        
        #修改商品的商品名称
        #生成随机后缀
        random_int = random.randint(1,999)
        sleep(3)
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[1]/div/input[2]').clear()
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品' + str(random_int))
        
        #修改商品的商品名称
        code = ''.join(str(random.randint(0, 9)) for _ in range(6))
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[2]/div/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[2]/div/input').send_keys(code)
        
        
        #修改第一个商品分类（下拉框第一个）
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(2)       
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        
        
        #修改第二个商品分类（下拉框第二个）
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[3]').click()
        
        #修改第三个商品分类（下拉框第二个）
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[3]').click()
        
        #修改商品主图，清除原来的主图
        delete = self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[1]/img') # 控件所在元素位置
        ActionChains(self.browser).move_to_element(delete).perform() # 将鼠标移动待元素上
        sleep(2)
        
        #清除原图
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[1]/a').click()
        sleep(2)
        
        #添加新图片
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(2)
        
        #定位上传图片子框架
        upload_img_iframe_2 = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(upload_img_iframe_2)
        
        #上传图片
        self.test_upload_img('C:\\Users\\韦帮愫\\Desktop\\software-testing\\验证码.png')
        
        
        # 先回到主框架-》商品子框架-》商品编辑子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_edit_iframe)
        
        
        
        # 点击价格库存
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(2)
        
        # 定位价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').send_keys('9999')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('1999')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').send_keys('99')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys('1')
        sleep(2)
        
        #子框架-》商品子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        
        #点击保存
        self.browser.find_element(By.XPATH, '/html/body/div[3]/div[3]/a[1]').click()
        
        
    def test_logout(self):
        self.browser.switch_to.default_content()
        #鼠标悬停
        dt = self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        
        
if __name__ == '__main__':
    likeShopTest = LikeShopTest()
    #登陆
    likeShopTest.test_login()
    #获取商品列表
    # likeShopTest.tset_shop_list()
    #添加商品
    #likeShopTest.test_shop_add()
    #编辑商品（上架的会修改失败，不要扣我分啊老师！！）
    likeShopTest.test_shop_edit()
    #商品上架
    #likeShopTest.test_shop_sj()
    #退出登陆
    likeShopTest.test_logout()